﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A2DFramework.Infrastructure.Mvc.VerficationCode
{
    public class VerificationCodeAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            string verifyCode = value == null ? "" : (string)value;
            if (verifyCode == null || verifyCode.Trim().Length == 0)
                return ValidationResult.Success;

            if (verifyCode != VerificationCodeHelper.GetCurrentCode())
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

            return ValidationResult.Success;
        }
    }
}
